package com.hssy.wululuaplan.controller;

import com.hssy.wululuaplan.common.Result;
import com.hssy.wululuaplan.form.LoginParam;
import com.hssy.wululuaplan.service.LoginService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
@Api(tags = "登录")
@RestController
@RequestMapping("/login")
public class LoginController {
    // @GetMapping
    // public Result<String> login(
    //         String username,
    //         String password,
    //         HttpServletRequest request) {
    //     if ("admin".equals(username) && "123456".equals(password)){
    //         HttpSession session = request.getSession();
    //         session.setAttribute("user", username);
    //         return Result.success("神奇女侠");
    //     }else {
    //         return Result.error();
    //     }
    // }
    // @GetMapping("/logout")
    // public Result<String> logout(HttpServletRequest request) {
    //     request.getSession().removeAttribute("user");
    //     return Result.success();
    // }

    @Resource
    private LoginService loginService;

    @PostMapping
    public Result<String> login(@RequestBody LoginParam loginParam) {
        String token = loginService.login(loginParam);
        return Result.success(token);
    }

    @GetMapping("/logout")
    public Result<String> logout(@RequestHeader("Authorization") String token) {
        loginService.logout(token);
        return Result.success();
    }

}
